﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5GenericLists
{
    class GenericList<T>
    {
        private T[] members;
        private int used = 0;


        public GenericList(int startCapacity)
        {
            this.members = new T[startCapacity];
        }

        public void DoubleCapacity()
        {
            T[] tempArray = new T[members.Length * 2];
            Array.Copy(members, 0, tempArray, 0, members.Length);
            members = tempArray;
        }

        public void Add(T element)
        {
            if (this.used >= members.Length)
            {
                DoubleCapacity();
            }
            members[used] = element;
            used++;
        }

        public void InsertElementAt(T element, int position)
        {
            if (members.Length == this.used)
            {
                DoubleCapacity();
            }
            T[] tempArrAfterPos = new T[members.Length - position - 1];
            Array.Copy(members, position, tempArrAfterPos, 0, tempArrAfterPos.Length);
            members[position] = element;
            Array.Copy(tempArrAfterPos, 0, members, position + 1, tempArrAfterPos.Length);
            used++;
        }

        public void RemoveElementFrom(int position)
        {
            T[] tempArrAfterPos = new T[members.Length - position - 1];
            Array.Copy(members, position + 1, tempArrAfterPos, 0, tempArrAfterPos.Length);
            Array.Copy(tempArrAfterPos, 0, members, position, tempArrAfterPos.Length);
            members[members.Length - 1] = default(T);
            used--;
        }
    }
}
